#!/usr/bin/env perl
#-----------------------------------------------------------------------------------------------
#
# build-namelist
#
# This script builds the namelists for CISM
#
# Date        Contributor      Modification
# -------------------------------------------------------------------------------------------
# 2009-10-30  Kluzek           Original version
#--------------------------------------------------------------------------------------------

use strict;
#use warnings;
#use diagnostics;

use Cwd;
use English;
use Getopt::Long;
use IO::File;
#-----------------------------------------------------------------------------------------------

sub usage {
    die <<EOF;
SYNOPSIS
     build-namelist [options]
OPTIONS
     -case "name"             Case identifier up to 32 characters
     -csmdata "dir"           Root directory of CCSM input data.
                              Can also be set by using the CSMDATA environment variable.
     -d "directory"           Directory where output namelist file will be written
                              Default: current working directory.
     -help [or -h]            Print usage to STDOUT.
     -infile "filepath"       Specify a file containing namelists to read values from.
     -inputdata "filepath"    Writes out a list containing pathnames for required input datasets in
                              file specified.
     -mask "landmask"         Type of land-mask (default, navy, gx3v5, gx1v5 etc.)
     -namelist "namelist"     Specify namelist settings directly on the commandline by supplying 
                              a string containing FORTRAN namelist syntax, e.g.,
                                 -namelist "&cism_inparm dt=1800 /"
     -res "resolution"        Specify land model horizontal grid.  Use nlatxnlon for spectral grids;
                              dlatxdlon for fv grids (dlat and dlon are the grid cell size
    			      in degrees for latitude and longitude respectively)
     -s                       Turns on silent mode - only fatal messages issued.
     -test                    Enable checking that input datasets exist on local filesystem.
     -verbose [or -v]         Turn on verbose echoing of informational messages.
     -use_case "case"         Specify a use case which will provide default values.


Note: The precedence for setting the values of namelist variables is (highest to lowest):
      0. values set from a use-case scenario, e.g., -use_case
      1. namelist values set by specific command-line options, i.e., -d 
      2. values set on the command-line using the -namelist option,
      3. values read from the file specified by -infile,
      4. values from the namelist defaults file.
EOF
}

#-----------------------------------------------------------------------------------------------
# Set the directory that contains the CISM configuration scripts.  If the command was
# issued using a relative or absolute path, that path is in $ProgDir.  Otherwise assume the
# command was issued from the current working directory.

(my $ProgName = $0) =~ s!(.*)/!!;      # name of this script
my $ProgDir = $1;                      # name of directory containing this script -- may be a
                                       # relative or absolute path, or null if the script is in
                                       # the user's PATH
my $cmdline = "@ARGV";                 # Command line arguments to script
my $cwd = getcwd();                    # current working directory
my $cfgdir;                            # absolute pathname of directory that contains this script
my $nm = "ProgName::";                 # name to use if script dies
if ($ProgDir) { 
    $cfgdir = absolute_path($ProgDir);
} else {
    $cfgdir = $cwd;
}

my $outdirname = "$cwd";                  # Default name of output directory name

#-----------------------------------------------------------------------------------------------

# Process command-line options.

my %opts = ( 
	     csmdata         => undef,
	     config          => "config_definition.xml",
	     help            => 0,
	     dir             => $outdirname,
	     res             => "default",
	     silent          => 0,
             mask            => "default",
	     test            => 0,
	    );

GetOptions(
    "case=s"                    => \$opts{'case'},
    "csmdata=s"                 => \$opts{'csmdata'},
    "d|d=s"                     => \$opts{'dir'},
    "h|help"                    => \$opts{'help'},
    "infile=s"                  => \$opts{'infile'},
    "inputdata=s"               => \$opts{'inputdata'},
    "mask=s"                    => \$opts{'mask'},
    "namelist=s"                => \$opts{'namelist'},
    "res=s"                     => \$opts{'res'},
    "s|silent"                  => \$opts{'silent'},
    "test"                      => \$opts{'test'},
    "use_case=s"                => \$opts{'use_case'},
    "v|verbose"                 => \$opts{'verbose'},
)  or usage();

# Give usage message.
usage() if $opts{'help'};

# Check for unparsed arguments
if (@ARGV) {
    print "ERROR: unrecognized arguments: @ARGV\n";
    usage();
}

# Define print levels:
# 0 - only issue fatal error messages
# 1 - only informs what files are created (default)
# 2 - verbose
my $print = 1;
if ($opts{'silent'})  { $print = 0; }
if ($opts{'verbose'}) { $print = 2; }
my $eol = "\n";

if ($print>=2) { print "Setting CISM configuration script directory to $cfgdir$eol"; }

# Add the location of the use case defaults files to the options hash
$opts{'use_case_dir'} = "$cfgdir/use_cases";

# Validate some of the commandline option values.
validate_options("commandline", \%opts);

#-----------------------------------------------------------------------------------------------
# Make sure we can find required perl modules, definition, and defaults files.
# Look for them under the directory that contains the configure script.

# The root directory for the input data files must be specified.

# The XML::Lite module is required to parse the XML files.
(-f "$cfgdir/../../../../scripts/ccsm_utils/Tools/perl5lib/XML/Lite.pm")  or  die <<"EOF";
** $ProgName - Cannot find perl module \"XML/Lite.pm\" in directory 
    \"$cfgdir/../../../../scripts/ccsm_utils/Tools/perl5lib\" **
EOF

# The namelist definition file contains entries for all namelist variables that
# can be output by build-namelist.
my $glc_nl_definition_file = "$cfgdir/namelist_definition_cism.xml";
(-f "$glc_nl_definition_file")  or  die <<"EOF";
** $ProgName - Cannot find namelist definition file \"$glc_nl_definition_file\" **
EOF
if ($print>=2) { print "Using namelist definition file $glc_nl_definition_file$eol"; }

my $nl_definition_file = "$cfgdir/namelist_definition_overall.xml";
(-f "$nl_definition_file")  or  die <<"EOF";
** $ProgName - Cannot find namelist definition file \"$nl_definition_file\" **
EOF
if ($print>=2) { print "Using namelist definition file $nl_definition_file$eol"; }

# The Build::NamelistDefinition module provides utilities to validate that the output
# namelists are consistent with the namelist definition file
(-f "$cfgdir/../../../../scripts/ccsm_utils/Tools/perl5lib/Build/NamelistDefinition.pm")  or  die <<"EOF";
** $ProgName - Cannot find perl module \"Build/NamelistDefinition.pm\" in directory 
    \"$cfgdir/../../../../scripts/ccsm_utils/Tools/perl5lib\" **
EOF

# The namelist defaults file contains default values for all required namelist variables.
my $glc_nl_defaults_file = "$cfgdir/namelist_defaults_cism.xml";
(-f "$glc_nl_defaults_file")  or  die <<"EOF";
** $ProgName - Cannot find namelist defaults file \"$glc_nl_defaults_file\" **
EOF
if ($print>=2) { print "Using namelist defaults file $glc_nl_defaults_file$eol"; }

# The namelist defaults file contains default values for all required namelist variables.
my $nl_defaults_file = "$cfgdir/namelist_defaults_overall.xml";
(-f "$nl_defaults_file")  or  die <<"EOF";
** $ProgName - Cannot find namelist defaults file \"$nl_defaults_file\" **
EOF
if ($print>=2) { print "Using namelist defaults file $nl_defaults_file$eol"; }

# The Build::NamelistDefaults module provides a utility to obtain default values of namelist
# variables based on finding a best fit with the attributes specified in the defaults file.
(-f "$cfgdir/../../../../scripts/ccsm_utils/Tools/perl5lib/Build/NamelistDefaults.pm")  or  die <<"EOF";
** $ProgName - Cannot find perl module \"Build/NamelistDefaults.pm\" in directory 
    \"$cfgdir/../../../../scripts/ccsm_utils/Tools/perl5lib\" **
EOF

# The Build::Namelist module provides utilities to parse input namelists, to query and modify
# namelists, and to write output namelists.
(-f "$cfgdir/../../../../scripts/ccsm_utils/Tools/perl5lib/Build/Namelist.pm")  or  die <<"EOF";
** $ProgName - Cannot find perl module \"Build/Namelist.pm\" in directory 
    \"$cfgdir/../../../../scripts/ccsm_utils/Tools/perl5lib\" **
EOF

#-----------------------------------------------------------------------------------------------
# Add $cfgdir/perl5lib to the list of paths that Perl searches for modules
my @dirs = ( $cfgdir, "$cfgdir/../../../../scripts/ccsm_utils/Tools/perl5lib");
unshift @INC, @dirs;
require XML::Lite;
require Build::Config;
require Build::NamelistDefinition;
require Build::NamelistDefaults;
require Build::Namelist;
require Streams::Template;
#-----------------------------------------------------------------------------------------------
# Create a configuration object from the CLM config_cache.xml file. 
my $cfg = Build::Config->new( $opts{'config'} );

# Create a namelist definition object.  This object provides a method for verifying that the
# output namelist variables are in the definition file, and are output in the correct
# namelist groups.
my $glc_definition = Build::NamelistDefinition->new( $glc_nl_definition_file );
$glc_definition->add( $nl_definition_file );

# Create a namelist defaults object.  This object provides default values for variables
# contained in the input defaults file.  The configuration object provides attribute
# values that are relevent for the CISM executable for which the namelist is being produced.
my $glc_defaults = Build::NamelistDefaults->new( $glc_nl_defaults_file, $cfg );
$glc_defaults->add( $nl_defaults_file, $cfg );

# Create an empty namelist object.  Add values to it in order of precedence.
my $glc_nl = Build::Namelist->new();

# Check that the CCSM inputdata root directory has been specified.  This must be
# a local or nfs mounted directory.
my $inputdata_rootdir = undef;
if (defined($opts{'csmdata'})) {
    $inputdata_rootdir = $opts{'csmdata'};
}
elsif (defined $ENV{'CSMDATA'}) {
    $inputdata_rootdir = $ENV{'CSMDATA'};
}
else {
    die "$ProgName - ERROR: CCSM inputdata root directory must be specified by either -csmdata argument\n" .
	" or by the CSMDATA environment variable. :";
}

if ($opts{'test'}) {
    (-d $inputdata_rootdir)  or  die <<"EOF";
** $ProgName - CCSM inputdata root is not a directory: \"$inputdata_rootdir\" **
EOF
}

if ($print>=2) { print "CCSM inputdata root directory: $inputdata_rootdir$eol"; }
#-----------------------------------------------------------------------------------------------

# Some regular expressions...
###my $TRUE  = qr/\.true\./i;
###my $FALSE = qr/\.false\./i;
# **N.B.** the use of qr// for precompiling regexps isn't supported until perl 5.005.
my $TRUE  = '\.true\.';
my $FALSE = '\.false\.';

#-----------------------------------------------------------------------------------------------

# Process the user input in order of precedence.  At each point we'll only add new
# values to the namelist and not overwrite previously specified specified values which
# have higher precedence.

# Process the -use_case arg.

if (defined $opts{'use_case'}) {

    # The use case definition is contained in an xml file with the same format as the defaults file.
    # Create a new NamelistDefaults object.
    my $uc_defaults = Build::NamelistDefaults->new("$opts{'use_case_dir'}/$opts{'use_case'}.xml", $cfg );

    # Loop over the variables specified in the use case.
    # Add each one to the namelist.
    my @vars = $uc_defaults->get_variable_names();
    foreach my $var (@vars) {
	my $val = $uc_defaults->get_value($var);

        print "adding use_case $opts{'use_case'} defaults for var $var with val $val \n";

	if ($val) {
	    add_default($glc_nl, $var, $glc_definition, $glc_defaults, 'val'=>$val);
	}
	else {
	    die "$ProgName - ERROR: No default value found for variable $var in '-use_case' $opts{'use_case'}.\n $@";
	}
    }
}

# Process the commandline args that provide specific namelist values.

# Process the -namelist arg.

if (defined $opts{'namelist'}) {
    # Parse commandline namelist
    my $nl_arg = Build::Namelist->new($opts{'namelist'});

    # Validate input namelist -- trap exceptions
    my $nl_arg_valid;
    eval { $nl_arg_valid = $glc_definition->validate($nl_arg); };
    if ($@) {
	die "$ProgName - ERROR: Invalid namelist variable in commandline arg '-namelist'.\n $@";
    }

    # Merge input values into namelist.  Previously specified values have higher precedence
    # and are not overwritten.
    $glc_nl->merge_nl($nl_arg_valid);
}

# Process the -infile arg.

if (defined $opts{'infile'}) {
    # Parse namelist input from a file
    my $nl_infile = Build::Namelist->new($opts{'infile'});

    # Validate input namelist -- trap exceptions
    my $nl_infile_valid;
    eval { $nl_infile_valid = $glc_definition->validate($nl_infile); };
    if ($@) {
	die "$ProgName - ERROR: Invalid namelist variable in '-infile' $opts{'infile'}.\n $@";
    }

    # Merge input values into namelist.  Previously specified values have higher precedence
    # and are not overwritten.
    $glc_nl->merge_nl($nl_infile_valid);
}


#-----------------------------------------------------------------------------------------------

my $val;

# Obtain default values for the following build-namelist input arguments

my $val;
my $res;
my $group;
my $var;

$var = "res";
if ( $opts{$var} ne "default" ) {
    $val = $opts{$var};
} else {
    $val= $glc_defaults->get_value($var);
}
$res = $val;
if ($print>=2) { print "CISM atm resolution is $res $eol"; }
$opts{$var} = $val;
$val = &quote_string( $res );
$group = $glc_definition->get_group_name($var);
$glc_nl->set_variable_value($group, $var, $val);
if (  ! $glc_definition->is_valid_value( $var, $val ) ) {
   my @valid_values   = $glc_definition->get_valid_values( $var );
   die "$nm $var has a value ($val) that is NOT valid. Valid values are: @valid_values\n";
}

$var = "mask";
if ( $opts{$var} ne "default" ) {
    $val = $opts{$var};
} else {
    $val = $glc_defaults->get_value($var);
}
my $mask = $val;
$opts{'mask'} = $mask;
$val = &quote_string( $val );
$group = $glc_definition->get_group_name($var);
$glc_nl->set_variable_value($group, $var, $val);
if (  ! $glc_definition->is_valid_value( $var, $val ) ) {
   my @valid_values   = $glc_definition->get_valid_values( $var );
   die "$nm $var has a value ($val) that is NOT valid. Valid values are: @valid_values\n";
}
if ($print>=2) { print "CISM land mask is $mask $eol"; }

# Add default values for required namelist variables that have not been previously set.
# This is done either by using the namelist default object, or directly with inline logic.

##############################
# namelist group: cism_in    #
##############################

my $case = $glc_nl->get_value('case_name');

my $start_ymd = $glc_defaults->get_value("start_ymd" );
my $start_tod = $glc_defaults->get_value("start_tod" );

add_default($glc_nl, 'paramfile',       $glc_definition, $glc_defaults);
add_default($glc_nl, 'horiz_grid_opt',  $glc_definition, $glc_defaults);
add_default($glc_nl, 'horiz_grid_file', $glc_definition, $glc_defaults, 
                'mask'=>$mask,'hgrid'=>$res );
add_default($glc_nl, 'topo_varname',    $glc_definition, $glc_defaults,    
                'mask'=>$mask,'hgrid'=>$res);
add_default($glc_nl, 'date_separator',  $glc_definition, $glc_defaults );
#add_default($glc_nl, 'runic',           $glc_definition, $glc_defaults, 'val'=>$case );
add_default($glc_nl, 'diri',            $glc_definition, $glc_defaults);
add_default($glc_nl, 'diro',            $glc_definition, $glc_defaults);
add_default($glc_nl, 'logfile',         $glc_definition, $glc_defaults);
add_default($glc_nl, 'stop_option',     $glc_definition, $glc_defaults);

my $year  = int( $start_ymd / 10000 );
my $month = int( ($start_ymd - $year * 10000) / 100 );
my $day   = $start_ymd - $year * 10000 - $month*100;

print "year = $year $month = $month day = $day\n";

my $hour = int( $start_tod / 3600 );
my $min  = int( ($start_tod - $hour*3600) / 60 );
my $sec  = $start_tod - $hour * 3600 - $min * 60;

add_default($glc_nl, 'iyear0',   $glc_definition, $glc_defaults, 'val'=>$year  );
add_default($glc_nl, 'imonth0',  $glc_definition, $glc_defaults, 'val'=>$month );
add_default($glc_nl, 'iday0',    $glc_definition, $glc_defaults, 'val'=>$day   );
add_default($glc_nl, 'ihour0',   $glc_definition, $glc_defaults, 'val'=>$hour  );
add_default($glc_nl, 'iminute0', $glc_definition, $glc_defaults, 'val'=>$min   );
add_default($glc_nl, 'isecond0',  $glc_definition, $glc_defaults, 'val'=>$sec   );

my $inputgrid  = $glc_defaults->get_value("inputgrid");
$inputgrid     = set_abs_filepath($inputgrid, $inputdata_rootdir);
my $cmd = "cp -pf $inputgrid .";
print "$cmd\n";
system( "$cmd" );

#-----------------------------------------------------------------------------------------------
# Validate that the entire resultant namelist is valid
#
$glc_definition->validate($glc_nl);


#-----------------------------------------------------------------------------------------------
# Write output

my $note = "Comment:\n" . 
           "This namelist was created using the following command-line:\n" .
           "    $cfgdir/$ProgName $cmdline\n" .
           "For help on options use: $cfgdir/$ProgName -help";

# Glacier component
my @groups = qw(files_nml grid_nml time_manager_nml);
my $outfile = "$opts{'dir'}/cism_in";
$glc_nl->write($outfile, 'groups'=>\@groups, 'note'=>"$note" );
if ($print>=2) { print "Writing glacier namelist to $outfile $eol"; }
@groups = qw(modelio);
$outfile = "$opts{'dir'}/glc_modelio.nml";
$glc_nl->write($outfile, 'groups'=>\@groups, 'note'=>"$note" );
if ($print>=2) { print "Writing glacier namelist to $outfile $eol"; }
$outfile = "$opts{'dir'}/cismx_in";
$res =~ /([0-9]+)x([0-9]+)/;
my $idir = $1;
my $jdir = $2;
my $fh = IO::File->new($outfile, '>') or die "** $ProgName - can't open glcx input file: $outfile\n";
print $fh <<EOF;
$idir                       ! i-direction global dimension
$jdir                       ! j-direction global dimension
2                           ! decomp_type 1=1d-by-lat, 2=1d-by-lon, 3=2d, 4=2d evensquare, 11=segmented
0                           ! num of pes for i (type 3 only)
0                           ! length of segments (type 4 only)
EOF
$fh->close();
if ($print>=2) { print "Writing glacier input file to $outfile $eol"; }
#-----------------------------------------------------------------------------------------------

# Output input dataset list.
if ($opts{'inputdata'}) {
    check_input_files($glc_nl,$inputdata_rootdir,$opts{'inputdata'});
}

# END OF MAIN SCRIPT
#===============================================================================================

sub add_default {

# Add a value for the specified variable to the specified namelist object.  The variables
# already in the object have the higher precedence, so if the specified variable is already
# defined in the object then don't overwrite it, just return.
#
# This method checks the definition file and adds the variable to the correct
# namelist group.
#
# The value can be provided by using the optional argument key 'val' in the
# calling list.  Otherwise a default value is obtained from the namelist
# defaults object.  If no default value is found this method throws an exception
# unless the 'nofail' option is set true.
#
# Example 1: Specify the default value $val for the namelist variable $var in namelist
#            object $nl:
#
#  add_default($nl, $var, $definition, $defaults, 'val'=>$val)
#
# Example 2: Add a default for variable $var if an appropriate value is found.  Otherwise
#            don't add the variable
#
#  add_default($nl, $var, $definition, $defaults, 'nofail'=>1)
#
#
# ***** N.B. ***** This routine assumes the following variables are in package main::
#  $inputdata_rootdir -- CCSM inputdata root directory

    my $nl         = shift;    # namelist object
    my $var        = shift;    # name of namelist variable
    my $definition = shift;    # namelist definition
    my $defaults   = shift;    # namelist defaults
    my %opts       = @_;       # options

    # Query the definition to find which group the variable belongs to.  Exit if not found.
    my $group = $definition->get_group_name($var);
    unless ($group) {
	my $fname = $definition->get_file_name();
	die "$ProgName - ERROR: variable \"$var\" not found in namelist definition file $fname.\n";
    }

    # check whether the variable has a value in the namelist object -- if so then skip to end
    my $val = $nl->get_variable_value($group, $var);
    if (! defined $val) {

       # Look for a specified value in the options hash

       if (defined $opts{'val'}) {
	   $val = $opts{'val'};
       }
       # or else get a value from namelist defaults object.
       # Note that if the 'val' key isn't in the hash, then just pass anything else
       # in %opts to the get_value method to be used as attributes that are matched
       # when looking for default values.
       else {
	   $val = $defaults->get_value($var, \%opts);

       }

       # if no value is found then exit w/ error (unless 'nofail' option set)
       unless ( defined($val) ) {
	   unless ($opts{'nofail'}) {
	       die "$ProgName - No default value found for $var.\n" . 
                   "            Are defaults provided for this resolution and land mask?\n";
	   }
	   else {
	       return;
	   }
       }

       # query the definition to find out if the variable is an input pathname
       my $is_input_pathname = $definition->is_input_pathname($var);

       # The default values for input pathnames are relative.  If the namelist
       # variable is defined to be an absolute pathname, then prepend
       # the CCSM inputdata root directory.
       if (not defined $opts{'no_abspath'}) {
	   if (defined $opts{'set_abspath'}) {
	       $val = set_abs_filepath($val, $opts{'set_abspath'});
	   } else {
	       if ($is_input_pathname eq 'abs') {
		   $val = set_abs_filepath($val, $inputdata_rootdir);
	       }
	   }
       }

       # query the definition to find out if the variable takes a string value.
       # The returned string length will be >0 if $var is a string, and 0 if not.
       my $str_len = $definition->get_str_len($var);

       # If the variable is a string, then add quotes if they're missing
       if ($str_len > 0) {
	   $val = quote_string($val);
       }

       # set the value in the namelist
       $nl->set_variable_value($group, $var, $val);
    }

}

#-----------------------------------------------------------------------------------------------

sub check_input_files {

# For each variable in the namelist which is an input dataset, check to see if it
# exists locally.
#
# ***** N.B. ***** This routine assumes the following variables are in package main::
#  $glc_definition        -- the namelist definition object

    my $nl                = shift;     # namelist object
    my $inputdata_rootdir = shift;    # if false prints test, else creates inputdata file
    my $outfile           = shift;

    open(OUTFILE, ">>$outfile") if defined $inputdata_rootdir;

    # Look through all namelist groups
    my @groups = $nl->get_group_names();
    foreach my $group (@groups) {

	# Look through all variables in each group
	my @vars = $nl->get_variable_names($group);
	foreach my $var (@vars) {

	    # Is the variable an input dataset?
	    my $input_pathname_type = $glc_definition->is_input_pathname($var);

	    # If it is, check whether it exists locally and print status
	    if ($input_pathname_type) {

		# Get pathname of input dataset
		my $pathname = $nl->get_variable_value($group, $var);
		# Need to strip the quotes
		$pathname =~ s/['"]//g;

		if ($input_pathname_type eq 'abs') {
                    if ($inputdata_rootdir) {
                        $pathname =~ s:$inputdata_rootdir::;
                        print OUTFILE "$var = $pathname\n";
                    }
                    else {
		        if (-e $pathname) {  # use -e rather than -f since the absolute pathname
			                     # might be a directory
			    print "OK -- found $var = $pathname\n";
		        }
		        else {
			    print "NOT FOUND:  $var = $pathname\n";
		        }
                    }
		}
		elsif ($input_pathname_type =~ m/rel:(.+)/o) {
		    # The match provides the namelist variable that contains the
		    # root directory for a relative filename
		    my $rootdir_var = $1;
		    my $rootdir = $nl->get_variable_value($group, $rootdir_var);
		    $rootdir =~ s/['"]//g;
                    if ($inputdata_rootdir) {
                        $pathname = "$rootdir/$pathname";
                        $pathname =~ s:$inputdata_rootdir::;
                        print OUTFILE "$var = $pathname\n";
                    }
                    else {
		        if (-f "$rootdir/$pathname") {
			    print "OK -- found $var = $rootdir/$pathname\n";
		        }
		        else {
			    print "NOT FOUND:  $var = $rootdir/$pathname\n";
		        }
                    }
		}
	    }
	}
    }
    close OUTFILE if defined $inputdata_rootdir;
    return 0 if defined $inputdata_rootdir;
}


#-----------------------------------------------------------------------------------------------

sub set_abs_filepath {

# check whether the input filepath is an absolute path, and if it isn't then
# prepend a root directory

    my ($filepath, $rootdir) = @_;

    # strip any leading/trailing whitespace
    $filepath =~ s/^\s+//;
    $filepath =~ s/\s+$//;
    $rootdir  =~ s/^\s+//;
    $rootdir  =~ s/\s+$//;

    # strip any leading/trailing quotes
    $filepath =~ s/^['"]+//;
    $filepath =~ s/["']+$//;
    $rootdir =~ s/^['"]+//;
    $rootdir =~ s/["']+$//;

    my $out = $filepath;
    unless ( $filepath =~ /^\// ) {  # unless $filepath starts with a /
	$out = "$rootdir/$filepath"; # prepend the root directory
    }
    return $out;
}

#-----------------------------------------------------------------------------------------------


sub absolute_path {
#
# Convert a pathname into an absolute pathname, expanding any . or .. characters.
# Assumes pathnames refer to a local filesystem.
# Assumes the directory separator is "/".
#
  my $path = shift;
  my $cwd = getcwd();  # current working directory
  my $abspath;         # resulting absolute pathname

# Strip off any leading or trailing whitespace.  (This pattern won't match if
# there's embedded whitespace.
  $path =~ s!^\s*(\S*)\s*$!$1!;

# Convert relative to absolute path.

  if ($path =~ m!^\.$!) {          # path is "."
      return $cwd;
  } elsif ($path =~ m!^\./!) {     # path starts with "./"
      $path =~ s!^\.!$cwd!;
  } elsif ($path =~ m!^\.\.$!) {   # path is ".."
      $path = "$cwd/..";
  } elsif ($path =~ m!^\.\./!) {   # path starts with "../"
      $path = "$cwd/$path";
  } elsif ($path =~ m!^[^/]!) {    # path starts with non-slash character
      $path = "$cwd/$path";
  }

  my ($dir, @dirs2);
  my @dirs = split "/", $path, -1;   # The -1 prevents split from stripping trailing nulls
                                     # This enables correct processing of the input "/".

  # Remove any "" that are not leading.
  for (my $i=0; $i<=$#dirs; ++$i) {
      if ($i == 0 or $dirs[$i] ne "") {
	  push @dirs2, $dirs[$i];
      }
  }
  @dirs = ();

  # Remove any "."
  foreach $dir (@dirs2) {
      unless ($dir eq ".") {
	  push @dirs, $dir;
      }
  }
  @dirs2 = ();

  # Remove the "subdir/.." parts.
  foreach $dir (@dirs) {
    if ( $dir !~ /\.\./ ) {
        push @dirs2, $dir;
    } else {
        pop @dirs2;   # remove previous dir when current dir is ..
    }
  }
  if ($#dirs2 == 0 and $dirs2[0] eq "") { return "/"; }
  $abspath = join '/', @dirs2;
  return( $abspath );
}

#-------------------------------------------------------------------------------

sub valid_option {

    my ($val, @expect) = @_;
    my ($expect);

    $val =~ s/^\s+//;
    $val =~ s/\s+$//;
    foreach $expect (@expect) {
	if ($val =~ /^$expect$/i) { return $expect; }
    }
    return undef;
}

#-------------------------------------------------------------------------------

sub validate_options {

    my $source = shift;   # text string declaring the source of the options being validated
    my $opts   = shift;   # reference to hash that contains the options

    my ($opt, $old, @expect);
    
    # use_case
    $opt = 'use_case';
    if (defined $opts->{$opt}) {

	# create the @expect array by listing the files in $use_case_dir
	# and strip off the ".xml" part of the filename
	@expect = ();
	my @files = glob("$opts->{'use_case_dir'}/*.xml");
	foreach my $file (@files) {
	    $file =~ m{.*/(.*)\.xml};
	    push @expect, $1;
	}

	$old = $opts->{$opt};
	$opts->{$opt} = valid_option($old, @expect)
	    or die "$ProgName - invalid value of $opt ($old) specified in $source\n".
                   "expected one of: @expect\n";
    }

}

#-------------------------------------------------------------------------------

sub quote_string {
    my $str = shift;
    $str =~ s/^\s+//;
    $str =~ s/\s+$//;
    unless ($str =~ /^['"]/) {        #"'
        $str = "\'$str\'";
    }
    return $str;
}

#-------------------------------------------------------------------------------
